package org.csu.jpetstore.controller;

import org.csu.jpetstore.bo.AccountBO;
import org.csu.jpetstore.common.AccountStatusCode;
import org.csu.jpetstore.common.CommonResponse;
import org.csu.jpetstore.common.ResponseCode;
import org.csu.jpetstore.service.AccountService;
import org.csu.jpetstore.service.TokenService;
import org.csu.jpetstore.util.MD5Util;
import org.csu.jpetstore.util.MyToken;
import org.csu.jpetstore.vo.AccountVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


/**
 * @author huangzhangyan
 */
@Controller
@RequestMapping(value = "/accounts")
public class AccountController {

    @Autowired
    private AccountService accountService;
    @Autowired
    private TokenService tokenService;

    /**
     * 登录,登录成功返回token
     *
     * @param username 用户名
     * @param password 密码
     * @return CommonResponse token
     * @author huangzhangyan
     * @date 2022/4/24 14:32
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public CommonResponse<MyToken> login(@RequestParam String username, @RequestParam String password) {
        // 空校验
        if (null == username || null == password || "".equals(username) || "".equals(password)) {
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "用户名或密码不能为空");
        }

        // 密码加密
        password = MD5Util.getMD5String(password);

        // token获取
        MyToken token = accountService.login(username, password);
        if (null == token) {
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "用户名或密码不正确");
        }
        return CommonResponse.createForSuccess(token, "登录成功");
    }

    /**
     * 用于判断用户名是否存在,查询该用户名 如果存在返回非空的String 如果没有 返回null
     *
     * @param username 要判断是否存在的用户名
     * @return CommonResponse 存在返回非空的String;不存在返回null
     * @author huangzhangyan
     * @date 2022/4/24 14:34
     */
    @RequestMapping(value = "/{username}/username", method = RequestMethod.GET)
    @ResponseBody
    public CommonResponse<String> isUsernameExit(@PathVariable(name = "username") String username) {
        // 空校验
        if (null == username || "".equals(username)) {
            return CommonResponse.createForError("用户名为空");
        }
        if (accountService.isUsernameExist(username)) {
            return CommonResponse.createForSuccess(username, "用户名: " + username + " 存在");
        }
        return CommonResponse.createForSuccess(null, "用户名: " + username + " 不存在");
    }

    /**
     * 注册
     *
     * @author huangzhangyan
     * @date 2022/4/24 14:36
     */
    @RequestMapping(value = "/account", method = RequestMethod.POST)
    @ResponseBody
    public CommonResponse<Object> register(
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            @RequestParam("email") String email,
            @RequestParam("firstname") String firstname,
            @RequestParam("lastname") String lastname,
            @RequestParam("province") String province,
            @RequestParam("city") String city,
            @RequestParam("county") String county,
            @RequestParam("detailAddress") String detailAddress,
            @RequestParam("phone") String phone
    ) {
        // 空校验
        if (null == username || null == password || "".equals(username) || "".equals(password)) {
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "用户名或密码不能为空");
        }

        // 查询用户是否存在
        if (accountService.isUsernameExist(username)) {
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "用户名 " + username + " 存在");
        }

        AccountBO accountBO = new AccountBO();
        accountBO.setUsername(username);
        password = MD5Util.getMD5String(password);
        accountBO.setPassword(password);
        accountBO.setEmail(email);
        accountBO.setFirstname(firstname);
        accountBO.setLastname(lastname);
        accountBO.setProvince(province);
        accountBO.setCity(city);
        accountBO.setCounty(county);
        accountBO.setDetailAddress(detailAddress);
        accountBO.setPhone(phone);
        accountBO.setStatus(AccountStatusCode.AVAILABLE.getDescription());

        if (!accountService.insertAccount(accountBO)) {
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "注册失败");
        }
        return CommonResponse.createForSuccessMsg("注册成功");
    }

    /**
     * 修改用户信息
     *
     * @author huangzhangyan
     * @date 2022/4/24 14:36
     */
    @RequestMapping(value = "/account", method = RequestMethod.PUT)
    @ResponseBody
    public CommonResponse<Object> modify(
            @RequestHeader("Authorization") MyToken token,
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            @RequestParam("email") String email,
            @RequestParam("firstname") String firstname,
            @RequestParam("lastname") String lastname,
            @RequestParam("province") String province,
            @RequestParam("city") String city,
            @RequestParam("county") String county,
            @RequestParam("detailAddress") String detailAddress,
            @RequestParam("phone") String phone
    ) {

        // 空校验
        if (null == token) {
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "Token校验失败,请重新登录");
        }
        if (null == username || null == password || "".equals(username) || "".equals(password)) {
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "用户名或密码不能为空");
        }

        // Token校验
        if (!tokenService.isUserIdEqualToken(username, token)) {
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "Token校验失败,请重新登录");
        }


        // 查询用户是否存在
        // 前面已经Token校验了 不应该出现用户名不存在的情况
        if (!accountService.isUsernameExist(username)) {
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "用户名 " + username + " 不存在");
        }

        AccountBO accountBO = new AccountBO();
        accountBO.setUsername(username);
        // 密码加密
        password = MD5Util.getMD5String(password);
        accountBO.setPassword(password);
        accountBO.setEmail(email);
        accountBO.setFirstname(firstname);
        accountBO.setLastname(lastname);
        accountBO.setProvince(province);
        accountBO.setCity(city);
        accountBO.setCounty(county);
        accountBO.setDetailAddress(detailAddress);
        accountBO.setPhone(phone);
        accountBO.setStatus(AccountStatusCode.AVAILABLE.getDescription());

        accountService.modifyAccount(accountBO);
        return CommonResponse.createForSuccessMsg("修改成功");
    }

    /**
     * 获取用户信息
     *
     * @author huangzhangyan
     * @date 2022/4/24 14:40
     */
    @RequestMapping(value = "/account", method = RequestMethod.GET)
    @ResponseBody
    public CommonResponse<AccountVO> getAccount(
            @RequestHeader("Authorization") MyToken token,
            @RequestParam String username) {

        // 空校验
        if (null == token) {
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "Token校验失败,请重新登录");
        }
        if (null == username || "".equals(username)) {
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "用户名不能为空");
        }

        // Token校验
        if (!tokenService.isUserIdEqualToken(username, token)) {
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "Token校验失败,请重新登录");
        }

        AccountVO accountVO = AccountBO.toAccountVO(accountService.getAccount(username));
        return CommonResponse.createForSuccess(accountVO);
    }
}
